//
//  GeAutoUpdate.m
//  MClient
//
//  Created by mac on 12-11-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "GeAutoUpdate.h"

#define JSON_URL @"http://202.102.246.168:8080/Tongbu/cx1.jsp?partyId="


@implementation GeAutoUpdate
@synthesize clientEditionNum,updateUrl,partyId,requestUrl,returnData;


//    初始化方法
- (id)initWithClientEdition
{
    self = [super init];
    if (self) {
        // Custom initialization 自定义的方法
        NSLog(@"执行到自定义的方法");
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"MClientConstant" ofType:@"plist"];
        NSDictionary *dictionary = [[NSDictionary alloc]initWithContentsOfFile:path]; 
       
        clientEditionNum = [dictionary objectForKey:@"editionNumber"];
        
        
        
//        NSDictionary *partyDic = [dictionary objectForKey:@"LoginWithHidePartyId"];
//        partyId = [partyDic objectForKey:@"PartyId"];
        
        partyId = [dictionary objectForKey:@"updateName"];
       
        
        requestUrl = [NSString stringWithFormat:@"%@%@_iphone",JSON_URL,partyId];
        
//        NSLog(@"客户端的版本是:%@  会员名称是:%@   请求地址是:%@",clientEditionNum,partyId,requestUrl);

        
    }
    return self;
}


//异步从服务器获得 服务器版本号以及升级地址
-(void)getJSonDataFromServer{
    NSURL *url=[NSURL URLWithString:requestUrl];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:@"GET"];
    
    [request setDelegate:self];
    [request startAsynchronous];
}



-(void)requestFailed:(ASIHTTPRequest *)request{//请求出错
    NSLog(@"请求远程json服务出错.服务地址%@",requestUrl);
    //    NSLog(@"请求远程json服务出错.服务地址%@",JSON_URL);
}
-(void)requestFinished:(ASIHTTPRequest *)request{//请求数据完成
    
    NSString *responseString = [request responseString];
    NSLog(@"返回的客户端版本数据为:%@",responseString);
    
    NSArray *tempArray = [responseString componentsSeparatedByString:@"#"];
    
//    加入下面的判断增强程序健壮性
    int tempArrayLength = [tempArray count];
    if (tempArrayLength > 1) {
    
    NSString *hostEditionNum = [tempArray objectAtIndex:1];
    self.updateUrl = [tempArray objectAtIndex:2];
    self.updateUrl = [self.updateUrl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    self.updateUrl = [updateUrl stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    self.updateUrl = [updateUrl stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    
    if (clientEditionNum != nil || ![clientEditionNum isEqualToString:@""]) {
        
    
    
    int clientNum = [clientEditionNum intValue];
    int hostNum = [hostEditionNum intValue];
    
    if (clientNum < hostNum) {
        NSLog(@"执行升级 相关操作");
        
        BOOL res = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:self.updateUrl]];
        
        

        if (res) {
            NSLog(@"发出alert");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"检测到新版本"
															message:@"选择取消安装可以暂不更新!"
														   delegate:self
												  cancelButtonTitle:@"取消本次更新"
												  otherButtonTitles:@"开始更新",nil];
			[alert show];
			[alert release];
            
            
        }
    
    
    }
        
        }
    
    }
//    NSLog(@"服务器端的版本号码:%@  更新地址:%@",hostEditionNum,updateUrl);
    NSLog(@"---------------------------------------------------------------%@",updateUrl);
    
}


-(void) updateNow{

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.updateUrl]];

}



-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"单击第几个:%d",buttonIndex);
    if (buttonIndex == 1) {
        
        [self updateNow];
        
    }else if(buttonIndex == 0){
    
        return;
    
    }
    
}





@end
